/*
 * 文件名：CustManagerServiceImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.customer.custmanager.service.imp;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.customer.custmanager.dao.CustManagerDao;
import com.wootide.cms.service.customer.custmanager.pojo.ClientLevel;
import com.wootide.cms.service.customer.custmanager.pojo.Customer;
import com.wootide.cms.service.customer.custmanager.service.CustManagerService;
import com.wootide.cms.service.customer.custmanager.transaction.CustManagerTransaction;

public class CustManagerServiceImp implements CustManagerService
{
    
    // 注入运行日志
    private RunLog runLog;
    
    //用户事务层实例
    private CustManagerTransaction custManagerTransaction;
    
    //用户数据访问层实例
    private CustManagerDao custManagerDao;
    
    @Override
    public Result queryForPages(Page page, QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            //queryConditon = null == queryConditon? new QueryCondition() : queryConditon;
            //queryConditon.setObjClass(Customer.class);
            QueryResults queryResults = custManagerTransaction.queryForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query customer Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query customer Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query customer Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
  }
    
    @Override
    public Result addOneCustomer(Customer customer, HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("customer=" + customer);
        Result result = new Result();
        try
        {
            custManagerTransaction.addCustomer(customer);
            runLog.info("Add Customer Success! Input data:" + customer, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Add Customer Failed! Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add Customer Failed! Input Data:" + customer
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add Customer Failed!Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    public RunLog getRunLog()
    {
        return runLog;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public CustManagerTransaction getCustManagerTransaction()
    {
        return custManagerTransaction;
    }

    public void setCustManagerTransaction(
            CustManagerTransaction custManagerTransaction)
    {
        this.custManagerTransaction = custManagerTransaction;
    }

    public CustManagerDao getCustManagerDao()
    {
        return custManagerDao;
    }

    public void setCustManagerDao(CustManagerDao custManagerDao)
    {
        this.custManagerDao = custManagerDao;
    }

    @Override
    public Result viewOneCustomer(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);

        Result result = new Result();
        try
        {
            Customer customerByInfo = custManagerTransaction.viewOneCustomer(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(customerByInfo);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One OrderByInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod OrderByInfo Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One OrderByInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result queryCustomerLevel(QueryCondition queryConditon)
    {
     // 打印方法入口参数
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryConditon = null == queryConditon? new QueryCondition() : queryConditon;
            queryConditon.setObjClass(ClientLevel.class);
            QueryResults queryResults = custManagerTransaction.queryCustomerLevel(queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query customerLevel Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query customerLevel Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query customerLevel Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result addOneClientLevel(ClientLevel clientLevel,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("clientLevel=" + clientLevel);
        Result result = new Result();
        try
        {
            custManagerTransaction.addClientLevel(clientLevel);
            runLog.info("Add clientLevel Success! Input data:" + clientLevel, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Add clientLevel Failed! Input data:" + clientLevel, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add clientLevel Failed! Input Data:" + clientLevel
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //未知异常
            runLog.error("Add clientLevel Failed!Input data:" + clientLevel, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result delCustomers(Integer[] innerIndexs, HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            Customer customerInfo = null;
            try
            {
                // 调用删除路由事务层
                customerInfo = custManagerTransaction.delOneCustomer(innerIndex);
                runLog.info("Delete customerInfo Success! customerInfo = " + customerInfo,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete customerInfo Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete customerInfo Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete customerInfo Failed!  Innerindex = " + innerIndex,
                        e,request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }

    @Override
    public Result modOneCustomer(Customer customer, HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("customer=" + customer);
        Result result = new Result();
        try
        {
            Customer oldCustomer = custManagerTransaction.modOneCustomer(customer);
            runLog.info("Mod customer Success! Input data:" + customer
                    + "|oldDate:" + oldCustomer, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod customer Failed! Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod customer Failed! Input Data:" + customer
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod customer Failed!Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result viewOneLevel(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);

        Result result = new Result();
        try
        {
            ClientLevel clientLevelInfo = custManagerTransaction.viewOneLevel(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(clientLevelInfo);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One clientLevelInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod clientLevelInfo Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One clientLevelInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result modOneLevel(ClientLevel clientLevel,
            HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("clientLevel=" + clientLevel);
        Result result = new Result();
        try
        {
            ClientLevel oldClientLevel = custManagerTransaction.modOneLevel(clientLevel);
            runLog.info("Mod clientLevel Success! Input data:" + clientLevel
                    + "|oldDate:" + oldClientLevel, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod clientLevel Failed! Input data:" + clientLevel, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod clientLevel Failed! Input Data:" + clientLevel
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod clientLevel Failed!Input data:" + clientLevel, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result delClientLevels(Integer[] innerIndexs,
            HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            ClientLevel levelInfo = null;
            try
            {
                // 调用删除路由事务层
                levelInfo = custManagerTransaction.delOneClientLevel(innerIndex);
                runLog.info("Delete ClientLevel Success! ClientLevel = " + levelInfo,
                        request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete ClientLevel Failed! Innerindex = " + innerIndex,
                        e,
                        request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete ClientLevel Failed! Innerindex = " + innerIndex
                        + " Error Message:" + e.getMessage(), request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete ClientLevel Failed!  Innerindex = " + innerIndex,
                        e,request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }

    @Override
    public Result lostOneCustomer(Customer customer, HttpServletRequest request)
    {
     // 打印方法入口参数
        runLog.debug("customer=" + customer);
        Result result = new Result();
        try
        {
            Customer oldCustomer = custManagerTransaction.lostOneCustomer(customer);
            runLog.info("Mod customer Success! Input data:" + customer
                    + "|oldDate:" + oldCustomer, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod customer Failed! Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod customer Failed! Input Data:" + customer
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod customer Failed!Input data:" + customer, e, request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    @Override
    public Result queryLevelForPages(Page page, QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            //queryConditon = null == queryConditon? new QueryCondition() : queryConditon;
            //queryConditon.setObjClass(Customer.class);
            QueryResults queryResults = custManagerTransaction.queryLevelForPages(page,
                    queryConditon);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query clientLevel Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query clientLevel Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query clientLevel Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
}
